package com.teangtang.fly.service;

import com.teangtang.fly.dto.TicketDTO;
import com.teangtang.fly.entity.Flight;
import com.teangtang.fly.entity.MyOrder;
import com.teangtang.fly.entity.Ticket;
import com.teangtang.fly.entity.Traveller;
import com.teangtang.fly.repository.FlightRepository;
import com.teangtang.fly.repository.MyOrderRepository;
import com.teangtang.fly.repository.TicketRepository;
import com.teangtang.fly.repository.TravellerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.util.StringUtils;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;


import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TicketService {

    @Autowired
    private TicketRepository ticketRepository;
    @Autowired
    private FlightRepository flightRepository;
    @Autowired
    private TravellerRepository travellerRepository;
    @Autowired
    private MyOrderRepository myOrderRepository;
    public List<Ticket> getAllTickets() {
        return ticketRepository.findAll();
    }

    public Ticket getTicketById(Long id) {
        return ticketRepository.findById(id).orElse(null);
    }

    public Ticket createTicket(Ticket ticket) {
        return ticketRepository.save(ticket);
    }

    public boolean updateTicket(TicketDTO ticketDTO,int flag) {
        Ticket ticket = ticketRepository.findById(ticketDTO.getId()).orElse(null);
        if(ticketDTO.getFlightId()!= null){
            Flight flight = flightRepository.findById(ticketDTO.getFlightId()).orElse(null);
        ticket.setFlight(flight);
        }
        if(ticketDTO.getIdCard()!=null){
            Traveller traveller = travellerRepository.findById(ticketDTO.getIdCard()).orElse(null);
            ticket.setTraveller(traveller);
        }

        ticket.setSeatNumber(ticketDTO.getSeatNumber());
        if(ticketDTO.getOrderId()!= null){
            MyOrder myOrder = myOrderRepository.findById(ticketDTO.getOrderId()).orElse(null);
            ticket.setMyOrder(myOrder);
        }
        if(flag == 1){
            if(!ticket.getStatus().equals("0")){
                return false;
            }
            else{
                ticket.setStatus(ticketDTO.getStatus());
                ticketRepository.save(ticket);
                return true;
            }
        }
        ticket.setStatus(ticketDTO.getStatus());
        ticketRepository.save(ticket);
        return true;
    }

    /**
     * 模糊查询机票信息
     * @param id 机票编号
     * @param flightId 班次号
     * @param idCard 旅客证件号
     * @return 机票列表
     */
    public Page<TicketDTO> searchTickets(String id, String flightId, String idCard, Pageable pageable){
        Specification<Ticket> spec = (root, query, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (!id.isEmpty()) {
                predicates.add(criteriaBuilder.equal(root.get("id"), id));
            }
            if (!flightId.isEmpty()) {
                predicates.add(criteriaBuilder.equal(root.get("flight").get("flightId"), flightId));
            }
            if (!idCard.isEmpty()) {
                predicates.add(criteriaBuilder.like(root.get("traveller").get("idCard"), "%" + idCard + "%")); // 模糊匹配
            }
            return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
        };
        Page<Ticket> tickets = ticketRepository.findAll(spec, pageable);
        List<TicketDTO> tickDTOS = tickets.stream().map(ticket -> {
            TicketDTO ticketDTO = new TicketDTO();
            ticketDTO.setId(ticket.getId());
            ticketDTO.setFlightId(ticket.getFlight().getFlightId());
            if(ticket.getTraveller()!= null){
            ticketDTO.setIdCard(ticket.getTraveller().getIdCard());
            ticketDTO.setIdCardType(ticket.getTraveller().getIdCardType());
            }
            ticketDTO.setStatus(ticket.getStatus());
            ticketDTO.setSeatNumber(ticket.getSeatNumber());
            if(ticket.getMyOrder()!= null){
            ticketDTO.setOrderId(ticket.getMyOrder().getOrderId());
            }
            return ticketDTO;
        }).collect(Collectors.toList());
        return new PageImpl<>(tickDTOS, pageable, tickets.getTotalElements());
    }
    public void deleteTicket(Long id) {
        ticketRepository.deleteById(id);
    }
}
